#pragma once

//handles super basic mimetypes. returns "application/octet-stream" otherwise
class MimeTypes
{
public:
	static string GetType(const string& extension)
	{
		Init();
		if(types.find(extension)!=types.end())
			return types[extension];
		return "application/octet-stream";
	}
private:
	static void Init() //future improvement: handle many-to-one relationship
	{
		static bool bInit=false;
		if(bInit)
			return;
		types["html"]="text/html";
		types["htm"]="text/html";
		types["js"]="application/x-javascript";
		types["bmp"]="image/bmp";
		types["gif"]="image/gif";
		types["jpeg"]="image/jpeg";
		types["jpg"]="image/jpeg";
		types["tif"]="image/tiff";
		types["tiff"]="image/tiff";
		bInit=true;
	}
	static map<string,string> types;
};

//used information from http://www.jmarshall.com/easy/http/ about HTML specs for this class
class HTTPRequest
{
public:
	HTTPRequest(char* _request,int _length);
	HTTPRequest(const HTTPRequest& src);
	HTTPRequest& operator=(const HTTPRequest& src);
	virtual ~HTTPRequest();
	
	void GetResponse(char** response,int* length);
private:
	bool ParseRequest();
	void Tokenize(const string& str,vector<string>& tokens,const string& delimiters=" ");
	string GetExtension(const string& path);
	string GetHeaderValue(const string& key);
	string GetWebRootPath();
	string SanitizePath(const string& path);
	string ToString(int num);
	void WriteResponse(const string& response,char** buffer,int* length);

	char* requestBuffer;
	int requestLength;

	string type;
	string path;
	string protocol;
	map<string,string> headers;
	string content;
};
